<?php
/**
 * Interview Block
 */
use Hive\Ressource;
use Fox\Html\Wrapper;
use Fox\Dao\SqlFragment;
use Fox\Generator;
use Fox\Html\Input;
use Fox\Html\Block;
use Fox\Html\TextArea;
use Fox\Html\DropList;

$rid = !empty($params['rid']) ? $params['rid'] : null;
try {
	$contact = Fox\Dao::load(Ressource::className(),$rid);
} catch(Fox\Object\Exception\NotExist $e) {
	
}
$users = Xview::instance()->application()->users();
$wrapper = new Wrapper('wInterview','');

$block = new Block('bInterview');
$block->noLegend();
$block->setColumnAttr('labelWidth',array(1=> 120,2 => '60'));
$block->add(new Input('plan_date',true));
// Interview Status
$block->add(new DropList('interview_status'),2)
			->size(Input::SIZE_AUTO)
			->options(Hive\Contact\Interview\Status::loadActives(),'status',Generator::call('label'));

// Interview Types
$block->add(new DropList('interview_type',true))
			->size(Input::SIZE_AUTO)
			->options(Hive\Contact\Interview\Type::loadActives(),'type',
					Generator::call('label'));
// Users
//@TODO filter by authorisations
$users = Xview::instance()->application()->users();
$block->add(new DropList(array('usrnm','interviewer'),true,false,true,User::current()->getId()),2)
			->size(Input::SIZE_AUTO)
			->options($users,'idusr',Generator::call('name'));
$block->item('interviewer')->label(Msg(Hive::BUNDLE,Hive::LABEL_INTERVIEW_WIDTH));
// Mission / Workflow step
if(!empty($contact) && $contact->exists()) {
	$missions = $contact->missions();
	if(count($missions) > 0) {
		$block->add(new DropList('mission',true))
				->size(Input::SIZE_AUTO)
				->options($missions,'missionid',Generator::call('label'));
		$block->add(new DropList('workflow_step',true,false,false),2)
			->size(Input::SIZE_AUTO)
			->source(Generator::url('missions','api','workflow',array('rid' => $rid,'deepness' => 'steps','mode' => 'droplist')))
			->linkFrom($block->item('mission'),true);
		if(count($missions) == 1) {
			$block->item('mission')->initialize($missions[0]->missionid);
			// Find Current Contact Step
			$partnerLink = $missions[0]->partnerLink($rid);
			$workflow = $missions[0]->workflow();
			$block->item('workflow_step')
					->initialize($partnerLink->status)
					->options($workflow->steps(),'stepid',Generator::call('label'));
		}
	}
}
$wrapper->add($block);
// Comments
$blockP1 = new Block('bInterviewP1',Msg(Hive::BUNDLE,Hive::INTERVIEW_TEXT_PREPARATION));
$blockP1->collapsed(false);
$blockP1->add(new TextArea(array('comments','text1'),false,null,10,10))
		->noLabel()
		->colspan(2);
$wrapper->add($blockP1);
$blockP2 = new Block('bInterviewP2',Msg(Hive::BUNDLE,Hive::INTERVIEW_TEXT_NOTES));
$blockP2->collapsed(true);
$blockP2->add(new TextArea(array('text2','text2'),false,null,10,10))
		->noLabel()
		->colspan(2);
$wrapper->add($blockP2);
$blockP3 = new Block('bInterviewP3',Msg(Hive::BUNDLE,Hive::INTERVIEW_TEXT_SUMMARY));
$blockP3->collapsed(true);
$blockP3->add(new TextArea(array('text3','text3'),false,null,10,4))
		->noLabel()
		->colspan(2);
$wrapper->add($blockP3);
return $wrapper;